package cn.swut.day02.demo02;
import domain.Student;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManage {
    public static void main(String[] args) {
        Student stu = new Student();
        Scanner num = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        lo:
        while (true) {
            System.out.println("————————欢迎来到学生管理系统——————————");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");//需要判断学号是否存在
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            int choose = num.nextInt();
            switch (choose) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    removeStudent(list);
                    break;
                case 3:
                    changeStudent(list);
                    break;
                case 4:
                    queryStudent(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("输入有误，请在试");
            }
        }
    }
    private static void changeStudent(ArrayList<Student> list) {
        System.out.println("请输入需要修改的学号");
        Scanner sca = new Scanner(System.in);
        String sid = sca.next();
        int index = getIndex(list, sid);
        if (index == -1) {
            System.out.println("查无信息, 请重新输入");
        } else {
            System.out.println("请输入新的学生姓名:");
            String name = sca.next();
            System.out.println("请输入新的学生年龄:");
            int age = sca.nextInt();
            System.out.println("请输入新的学生生日:");
            String birthday = sca.next();
            // 封装为新的学生对象
            Student stu = new Student(sid, name, age, birthday);
            // 调用集合的set方法, 完成修改
            list.set(index, stu);
            System.out.println("修改成功!");
        }
    }
    public static void removeStudent(ArrayList<Student> list) {
        Scanner sca = new Scanner(System.in);
        System.out.println("请输入需要删除的学号");
        String sid = sca.next();
        int index = getIndex(list, sid);
        if (index == -1) {
            System.out.println("查无信息, 请重新输入");
        } else {
            list.remove(index);
            System.out.println("已经成功删除");
        }
    }
    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无信息, 请添加后重新查询");
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
        }
    }
    public static void addStudent(ArrayList<Student> list) {
//        学号具有唯一性，需要验证
        Scanner sca = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入学号");
            sid = sca.next();
            int index = getIndex(list, sid);
            if (index == -1) {
                break;
            }
        }
        System.out.println("请输入名字");
        String name = sca.next();
        System.out.println("请输入年龄");
        int age = sca.nextInt();
        System.out.println("请输入生日");
        String birthday = sca.next();
        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("添加成功");
    }
    //    定义一个判断学号是否正确，并返回下标的方法，方便删除和改变
    public static int getIndex(ArrayList<Student> list, String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getSid().equals(sid)) {
                return i;
            }
        }
        return index;
    }
}
